# Maintainer: Kyle Kauffman <kyle.j.kauffman@gmail.com>

_realname=ipython
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=7.23.0
pkgrel=1
pkgdesc="An enhanced Interactive Python shell (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://ipython.org/"
license=('BSD')
# Note: prompt_toolkit 2.0 is required for this series.
# There is some breakage between prompt_toolkit 1.0 and 2.0.
depends=("${MINGW_PACKAGE_PREFIX}-python-backcall"
         "${MINGW_PACKAGE_PREFIX}-python-colorama"
         "${MINGW_PACKAGE_PREFIX}-python-decorator"
         "${MINGW_PACKAGE_PREFIX}-python-jedi"
         "${MINGW_PACKAGE_PREFIX}-python-matplotlib-inline"
         "${MINGW_PACKAGE_PREFIX}-python-pickleshare"
         "${MINGW_PACKAGE_PREFIX}-python-prompt_toolkit"
         "${MINGW_PACKAGE_PREFIX}-python-pygments"
         "${MINGW_PACKAGE_PREFIX}-python-setuptools"
         "${MINGW_PACKAGE_PREFIX}-python-traitlets"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "winpty")
# new optional thing: ipyparallel
optdepends=("${MINGW_PACKAGE_PREFIX}-python-nose: for IPython's test suite"
            "${MINGW_PACKAGE_PREFIX}-python-qtconsole: for ipython qtconsole"
            "${MINGW_PACKAGE_PREFIX}-python-ipython-ipyparallel: for IPython.parallel")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-requests"
              "${MINGW_PACKAGE_PREFIX}-python-testpath"
              "${MINGW_PACKAGE_PREFIX}-python-pygments" 
              "${MINGW_PACKAGE_PREFIX}-python-jupyter-nbformat"
              "${MINGW_PACKAGE_PREFIX}-python-ipykernel"
              "${MINGW_PACKAGE_PREFIX}-python-numpy")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/ipython/ipython/archive/${pkgver}.tar.gz")
sha256sums=('008de5f95cc928ffaa24f0b7785400218bc9645ed2bd8923f7ab80da169435ea')

prepare() {
  cd "${srcdir}"
  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  msg "Python build for ${CARCH}"  
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  msg "Python test for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python -m test || warning "Tests fail"
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  # see https://github.com/ipython/ipython/issues/2057
  #export LC_ALL=en_US.UTF-8
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install \
     --prefix=${MINGW_PREFIX} --root=${pkgdir} --optimize=1 --skip-build

  find "${pkgdir}/" -name "*.pyc" -delete
  find "${pkgdir}/" -type d -empty -delete

  # setuptools doesnt see mingw-w64-python's readline
  find "${pkgdir}/" -name 'requires.txt' -delete

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.cmd; do
     sed -e "s|${PREFIX_WIN}/bin/||g" -i ${_f}
  done

  # For the command to work on the bash prompt, you must use
  # winpty so include that in the Shebang before the python command.
  # I don't like this but what I like really doesn't matter.
  for _f in iptest iptest3 ipython ipython3; do
     sed -e "s|/usr/bin/env |${MINGW_PREFIX}/|g" \
         -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
         -e "s|#!|#!winpty |" -i "${pkgdir}${MINGW_PREFIX}/bin/${_f}"
  done

  install -Dm644 docs/source/about/license_and_copyright.rst "${pkgdir}${MINGW_PREFIX}/share/licenses/python-ipython/LICENSE"
}
